﻿using System;
using UILib;
using UnityEngine;
using XMainClient.UI;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000F32 RID: 3890
	internal sealed class XMobaBillboardComponent : XComponent
	{
		// Token: 0x170035FA RID: 13818
		// (get) Token: 0x0600CE43 RID: 52803 RVA: 0x002FBEFC File Offset: 0x002FA0FC
		public override uint ID
		{
			get
			{
				return XMobaBillboardComponent.uuID;
			}
		}

		// Token: 0x0600CE44 RID: 52804 RVA: 0x002FBF14 File Offset: 0x002FA114
		public override void OnAttachToHost(XObject host)
		{
			base.OnAttachToHost(host);
			this._mobaDoc = XDocuments.GetSpecificDocument<XMobaBattleDocument>(XMobaBattleDocument.uuID);
			GameObject gameObject = XSingleton<XResourceLoaderMgr>.singleton.CreateFromPrefab(XMobaBillboardComponent.HPBAR_TEMPLATE, this._entity.EngineObject.Position, this._entity.EngineObject.Rotation, true, false);
			bool flag = gameObject != null;
			if (flag)
			{
				this._billboard = gameObject.transform;
			}
			this._uiDummy = (this._billboard.GetComponent("UIDummy") as IUIDummy);
			IUIRect parent = XSingleton<XGameUI>.singleton.HpbarRoot.UIComponent as IUIRect;
			XSingleton<UiUtility>.singleton.AddChild(parent, gameObject, XSingleton<XGameUI>.singleton.HpbarRoot);
		}

		// Token: 0x0600CE45 RID: 52805 RVA: 0x002FBFCC File Offset: 0x002FA1CC
		protected override void EventSubscribe()
		{
			base.RegisterEvent(XEventDefine.XEvent_BillboardShowCtrl, new XComponent.XEventHandler(this.OnShowCtrl));
			base.RegisterEvent(XEventDefine.XEvent_BuffChange, new XComponent.XEventHandler(this.OnBuffChange));
		}

		// Token: 0x0600CE46 RID: 52806 RVA: 0x002FBFFC File Offset: 0x002FA1FC
		public override void Attached()
		{
			base.Attached();
			this._bloodBar = (this._billboard.Find("Hpbar").GetComponent("XUIProgress") as IXUIProgress);
			this._bloodGrid = (this._billboard.Find("Hpbar/BloodGrid").GetComponent("UIBloodGrid") as IUIBloodGrid);
			this._bloodGrid.SetMAXHP((int)this._entity.Attributes.GetAttr(XAttributeDefine.XAttr_MaxHP_Total));
			this._indureBar = (this._billboard.Find("Indure").GetComponent("XUIProgress") as IXUIProgress);
			this._name = (this._billboard.Find("Name").GetComponent("XUILabelSymbol") as IXUILabelSymbol);
			this._level = (this._billboard.Find("Level").GetComponent("XUILabel") as IXUILabel);
			this._exp = (this._level.gameObject.transform.Find("frame").GetComponent("XUISprite") as IXUISprite);
			GameObject gameObject = this._billboard.transform.Find("BuffFrame").gameObject;
			DlgHandlerBase.EnsureCreate<XBuffMonitorHandler>(ref this._buffMonitor, gameObject, null, true);
			bool isPlayer = this._entity.IsPlayer;
			if (isPlayer)
			{
				this._bloodBar.SetForegroundColor(XMobaBillboardComponent.billboard_green);
			}
			else
			{
				bool flag = XSingleton<XEntityMgr>.singleton.IsOpponent(this._entity);
				if (flag)
				{
					this._bloodBar.SetForegroundColor(XMobaBillboardComponent.billboard_red);
				}
				else
				{
					this._bloodBar.SetForegroundColor(XMobaBillboardComponent.billboard_blue);
				}
			}
			bool isRole = this._entity.IsRole;
			if (isRole)
			{
				uint num = this._entity.IsPlayer ? XSingleton<XAttributeMgr>.singleton.XPlayerData.MilitaryRank : (this._entity.Attributes as XRoleAttributes).MilitaryRank;
				bool flag2 = num == 0U;
				if (flag2)
				{
					this.SetNameStr(this._entity.Attributes.Name);
				}
				else
				{
					MilitaryRankByExploit.RowData byMilitaryRank = XMilitaryRankDocument._militaryReader.GetByMilitaryRank(num);
					this.SetNameStr(XLabelSymbolHelper.FormatImage(XMobaBillboardComponent.MILITARY_ATLAS, byMilitaryRank.Icon) + this._entity.Attributes.Name);
				}
			}
			else
			{
				this._name.SetVisible(false);
			}
			bool flag3 = this._buffMonitor != null;
			if (flag3)
			{
				this._buffMonitor.InitMonitor(XSingleton<XGlobalConfig>.singleton.BuffMaxDisplayCountTeam, true, false);
			}
			this._heroHeight = this._entity.Height;
			this._billboard.transform.localScale = Vector3.one * XMobaBillboardComponent.k;
			this._uiDummy.alpha = 0f;
		}

		// Token: 0x0600CE47 RID: 52807 RVA: 0x002FC2C9 File Offset: 0x002FA4C9
		private void SetNameStr(string str)
		{
			this.NameStr = str;
			this._name.InputText = str;
		}

		// Token: 0x0600CE48 RID: 52808 RVA: 0x002FC2E0 File Offset: 0x002FA4E0
		private void DestroyGameObjects()
		{
			bool flag = this._billboard != null;
			if (flag)
			{
				XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroy(this._billboard.gameObject, true, false);
				this._billboard = null;
			}
		}

		// Token: 0x0600CE49 RID: 52809 RVA: 0x002FC31F File Offset: 0x002FA51F
		public override void OnDetachFromHost()
		{
			this._alwaysHide = 0;
			this.DestroyGameObjects();
			base.OnDetachFromHost();
		}

		// Token: 0x0600CE4A RID: 52810 RVA: 0x002FC338 File Offset: 0x002FA538
		private bool OnShowCtrl(XEventArgs e)
		{
			XBillboardShowCtrlEventArgs xbillboardShowCtrlEventArgs = e as XBillboardShowCtrlEventArgs;
			bool flag = xbillboardShowCtrlEventArgs.type == BillBoardHideType.Invalid;
			bool result;
			if (flag)
			{
				XSingleton<XDebug>.singleton.AddErrorLog("undefine billboard hide type. check code or contect pyc.", null, null, null, null, null);
				result = false;
			}
			else
			{
				bool show = xbillboardShowCtrlEventArgs.show;
				if (show)
				{
					this._alwaysHide &= ~(1 << XFastEnumIntEqualityComparer<BillBoardHideType>.ToInt(xbillboardShowCtrlEventArgs.type));
				}
				else
				{
					this._alwaysHide |= 1 << XFastEnumIntEqualityComparer<BillBoardHideType>.ToInt(xbillboardShowCtrlEventArgs.type);
				}
				result = true;
			}
			return result;
		}

		// Token: 0x0600CE4B RID: 52811 RVA: 0x002FC3C4 File Offset: 0x002FA5C4
		private bool OnBuffChange(XEventArgs args)
		{
			XBuffChangeEventArgs xbuffChangeEventArgs = args as XBuffChangeEventArgs;
			bool isRole = xbuffChangeEventArgs.entity.IsRole;
			if (isRole)
			{
				XBuffComponent buffs = xbuffChangeEventArgs.entity.Buffs;
				bool flag = buffs != null;
				if (flag)
				{
					this._buffMonitor.OnBuffChanged(buffs.GetUIBuffList());
				}
			}
			return true;
		}

		// Token: 0x0600CE4C RID: 52812 RVA: 0x002FC41C File Offset: 0x002FA61C
		private void SetBillBoardSameByMaster()
		{
			bool flag = XEntity.ValideEntity(this._entity.MobbedBy);
			if (flag)
			{
				XMobaBillboardComponent xmobaBillboardComponent = this._entity.MobbedBy.GetXComponent(XMobaBillboardComponent.uuID) as XMobaBillboardComponent;
				this._name.SetVisible(true);
				this._name.InputText = xmobaBillboardComponent.NameStr;
				this._level.SetText(xmobaBillboardComponent._level.GetText());
			}
		}

		// Token: 0x0600CE4D RID: 52813 RVA: 0x002FC494 File Offset: 0x002FA694
		public override void PostUpdate(float fDeltaT)
		{
			XEntity xentity = this._host as XEntity;
			bool flag = xentity == null;
			if (!flag)
			{
				bool flag2 = !this.InitByMaster && this._entity.MobbedBy != null;
				if (flag2)
				{
					this.InitByMaster = true;
					XOthersAttributes xothersAttributes = this._entity.Attributes as XOthersAttributes;
					bool sameBillBoardByMaster = xothersAttributes.SameBillBoardByMaster;
					if (sameBillBoardByMaster)
					{
						this.SetBillBoardSameByMaster();
					}
				}
				bool flag3 = !XEntity.ValideEntity(xentity);
				if (flag3)
				{
					this._uiDummy.alpha = 0f;
				}
				else
				{
					bool flag4 = this._alwaysHide != 0;
					if (flag4)
					{
						this._uiDummy.alpha = 0f;
					}
					else
					{
						XPlayer player = XSingleton<XEntityMgr>.singleton.Player;
						bool flag5 = player == null || player.EngineObject == null;
						if (flag5)
						{
							this.DestroyGameObjects();
						}
						else
						{
							float num = Vector3.Distance(xentity.EngineObject.Position, player.EngineObject.Position);
							bool flag6 = num > this._viewDistance;
							if (flag6)
							{
								bool flag7 = this._billboard != null;
								if (flag7)
								{
									this._uiDummy.alpha = 0f;
									return;
								}
							}
							bool flag8 = this._billboard != null;
							if (flag8)
							{
								this.UpdateHpBar();
							}
						}
					}
				}
			}
		}

		// Token: 0x0600CE4E RID: 52814 RVA: 0x002FC5EC File Offset: 0x002FA7EC
		private void UpdateHpBar()
		{
			bool flag = this._uiDummy.alpha == 0f;
			if (flag)
			{
				this._uiDummy.alpha = 1f;
			}
			float num = 0.2f;
			bool flag2 = !this._bloodBar.gameObject.activeSelf;
			if (flag2)
			{
				num -= 0.05f;
			}
			bool flag3 = !this._indureBar.gameObject.activeSelf;
			if (flag3)
			{
				num -= 0.05f;
			}
			Vector3 position = this._entity.EngineObject.Position;
			Vector3 position2 = new Vector3(position.x, position.y + this._heroHeight + num, position.z);
			this._billboard.position = position2;
			this._billboard.rotation = XSingleton<XScene>.singleton.GameCamera.Rotaton;
			bool flag4 = XSingleton<XEntityMgr>.singleton.Player != null;
			if (flag4)
			{
				float num2 = 6.27f;
				float num3 = Vector3.Distance(XSingleton<XScene>.singleton.GameCamera.UnityCamera.transform.position, this._billboard.position);
				float num4 = XMobaBillboardComponent.k * num3 / num2;
				this._billboard.localScale = new Vector3(num4, num4, num4);
			}
			bool isRole = this._entity.IsRole;
			if (isRole)
			{
				int num5;
				float fillAmount;
				this._mobaDoc.GetRoleLevelAndExp(this._entity.ID, out num5, out fillAmount);
				this._level.SetText(num5.ToString());
				this._exp.SetFillAmount(fillAmount);
			}
			double attr = this._entity.Attributes.GetAttr(XAttributeDefine.XAttr_MaxHP_Total);
			bool flag5 = this._bloodGrid.MAXHP != (int)attr;
			if (flag5)
			{
				this._bloodGrid.SetMAXHP((int)attr);
			}
			double num6 = this._entity.Attributes.GetAttr(XAttributeDefine.XAttr_CurrentHP_Basic);
			bool flag6 = num6 < 0.0;
			if (flag6)
			{
				num6 = 0.0;
			}
			this._bloodBar.value = (float)(num6 / attr);
			bool activeInHierarchy = this._indureBar.gameObject.activeInHierarchy;
			if (activeInHierarchy)
			{
				bool flag7 = this._secondbar_usage == BillboardUsage.MP;
				if (flag7)
				{
					double attr2 = this._entity.Attributes.GetAttr(XAttributeDefine.XAttr_MaxMP_Total);
					double attr3 = this._entity.Attributes.GetAttr(XAttributeDefine.XAttr_CurrentMP_Basic);
					bool flag8 = attr3 < attr2;
					if (flag8)
					{
						this._indureBar.value = (float)(attr3 / attr2);
					}
					else
					{
						this._indureBar.value = 1f;
					}
				}
			}
		}

		// Token: 0x04005BED RID: 23533
		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("Moba_Billboard");

		// Token: 0x04005BEE RID: 23534
		private XMobaBattleDocument _mobaDoc;

		// Token: 0x04005BEF RID: 23535
		private Transform _billboard = null;

		// Token: 0x04005BF0 RID: 23536
		private IUIDummy _uiDummy = null;

		// Token: 0x04005BF1 RID: 23537
		private IUIBloodGrid _bloodGrid = null;

		// Token: 0x04005BF2 RID: 23538
		private IXUIProgress _bloodBar;

		// Token: 0x04005BF3 RID: 23539
		private IXUIProgress _indureBar;

		// Token: 0x04005BF4 RID: 23540
		public IXUILabelSymbol _name;

		// Token: 0x04005BF5 RID: 23541
		public IXUILabel _level;

		// Token: 0x04005BF6 RID: 23542
		public IXUISprite _exp;

		// Token: 0x04005BF7 RID: 23543
		public string NameStr = "";

		// Token: 0x04005BF8 RID: 23544
		private XBuffMonitorHandler _buffMonitor;

		// Token: 0x04005BF9 RID: 23545
		private bool InitByMaster = false;

		// Token: 0x04005BFA RID: 23546
		private float _heroHeight = 10f;

		// Token: 0x04005BFB RID: 23547
		private static float k = 0.007f;

		// Token: 0x04005BFC RID: 23548
		private float _viewDistance = 10f;

		// Token: 0x04005BFD RID: 23549
		private int _alwaysHide = 0;

		// Token: 0x04005BFE RID: 23550
		public static string HPBAR_TEMPLATE = "UI/Billboard/MobaBillboard";

		// Token: 0x04005BFF RID: 23551
		private static readonly string MILITARY_ATLAS = "common/Billboard";

		// Token: 0x04005C00 RID: 23552
		public static readonly string billboardString_red = "[e8280c]";

		// Token: 0x04005C01 RID: 23553
		public static readonly string billboardString_green = "[53d103]";

		// Token: 0x04005C02 RID: 23554
		public static readonly string billboardString_blue = "[0aabd0]";

		// Token: 0x04005C03 RID: 23555
		public static readonly Color billboard_red = new Color32(232, 40, 12, byte.MaxValue);

		// Token: 0x04005C04 RID: 23556
		public static readonly Color billboard_green = new Color32(83, 209, 3, byte.MaxValue);

		// Token: 0x04005C05 RID: 23557
		public static readonly Color billboard_blue = new Color32(10, 171, 208, byte.MaxValue);

		// Token: 0x04005C06 RID: 23558
		private BillboardUsage _secondbar_usage = BillboardUsage.MP;
	}
}
